﻿using System;
using System.Linq;
using Bids.Data;
using Bids.Core.DomainObjects;
using System.Web;
using System.Web.Mvc;
using System.Transactions;

namespace Bids.Web.Controllers.Admin
{
    public class TaskController : BaseController<Task>
    {

        IRepository<Contribution> _iRpoContr;
        IRepository<Finance> _iRpoFinance;
        public TaskController(IRepository<Task> iRpoTask,
            IRepository<Contribution> iRpoContr,
            IRepository<Finance> iRpoFinance)
            : base(iRpoTask)
        {
            _iRpoContr = iRpoContr;
            _iRpoFinance = iRpoFinance;
        }
        [HttpPost]
        public JsonResult ConAmount(int contrId,int userId,int amount)
        {
            try
            {
                using (TransactionScope ts = new TransactionScope())
                {
                    var contr = _iRpoContr.Find(contrId);
                    contr.HasReword = true;
                    var finance = new Finance
                    {
                        Amount = amount,
                        CreateTime = DateTime.Now,
                        State = (int)FinanceStatus.完成,
                        Type = (int)FinanceType.收入,
                        UserId = userId,
                        Remark = "项目【"+ contr.Task.Name +"】分配酬劳"
                    };
                    _iRpoContr.Update(contr);
                    _iRpoFinance.Add(finance);
                    ts.Complete();
                }
                return Json("true", JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return Json(ex.Message, JsonRequestBehavior.AllowGet);
            }
        }

    }
}
